//
//  CarefullyChosenController.m
//  Emoticon
//
//  Created by ytkjs2 on 2020/3/26.
//  Copyright © 2020 ytk. All rights reserved.
//

#import "CarefullyChosenController.h"
#import "ListViewCell.h"
#import "WenZiViewController.h"

@interface CarefullyChosenController ()

@property (nonatomic, strong)NSMutableArray * dataSource;

@end

@implementation CarefullyChosenController

- (NSMutableArray *)dataSource
{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.title = @"精选壁纸";
    
    [self.carefullyCollectionView registerNib:[UINib nibWithNibName:@"ListViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"ListViewCell"];
    
    __weak typeof(self)weakSelf = self;
    
    [PictureData getRequestWithServiceUrl:Carefully_URL parameters:@{} success:^(id  _Nonnull responseObject) {
        NSArray * arr = [responseObject objectForKey:@"data"];
        int index = 0;
        for (NSDictionary * dic in arr) {
            MainPicModel * model = [MainPicModel mj_objectWithKeyValues:dic];
            [weakSelf.dataSource addObject:model];
            index ++;
            if (index == weakSelf.dataSource.count) {
                [weakSelf.carefullyCollectionView reloadData];
            }
        }
    } failure:^(NSError * _Nonnull error) {
        
    }];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ListViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ListViewCell" forIndexPath:indexPath];
    
    MainPicModel * model = _dataSource[indexPath.item];
    
    NSString * imgUrl = [model.image stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    [cell.IMG sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@"icon_placeholderImage"]];
    cell.IMG.contentMode = UIViewContentModeScaleAspectFill;
    cell.bgView.hidden = cell.lab.hidden = NO;
    cell.bgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
    cell.lab.text = model.name;
    return cell;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 6.5;//上下行
}


- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 6.5;//行距
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(6.5, 12, 6.4, 12 );//分别为上、左、下、右
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    CGFloat width = (rScreenWidth - 40)/3;
    
    return CGSizeMake(width,width * 2436/1370);//887, 1920
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MainPicModel * model = _dataSource[indexPath.item];
    WenZiViewController * wenziVC = [[WenZiViewController alloc]initWithNibName:@"WenZiViewController" bundle:nil];
    wenziVC.cate_id = model.cate_id;
    wenziVC.isWenList = NO;
    if (indexPath.item==0) {
        wenziVC.picType = 3;
    }
    wenziVC.title = model.name;
    wenziVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:wenziVC animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
